<template>
	<div class="cms-template-container">
		<el-card shadow="hover" class="mb10" v-auth="'cms:template:list'">
			<el-form :inline="true" :model="search">
				<el-form-item>
					<el-input v-model="search.name" placeholder="请输入模板名称" prefix-icon="el-icon-search"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button icon="el-icon-search" type="primary" @click="onTableSearch()">查询</el-button>
					<el-button icon="el-icon-refresh" @click="onResetSearch()">重置</el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<el-card shadow="hover">
			<el-button v-auth="'cms:template:add'" type="primary" class="ml10" icon="el-icon-document-add" @click="onOpenAddTemplate()">新增</el-button>
			<el-button v-auth="'cms:template:edit'" type="primary" class="ml10" icon="el-icon-edit" @click="onOpenEditTemplate()" :disabled="tableData.selections.length != 1">修改</el-button>
			<el-button v-auth="'cms:template:remove'" type="primary" class="ml10" icon="el-icon-delete" @click="onTabelRowsDel()" :disabled="tableData.selections.length <= 0">删除</el-button>
			<el-table ref="tableTemplateRef" :data="tableData.data" @selection-change="onTableSelectionChange" style="width: 100%" row-key="id">
				<el-table-column type="selection" align="cneter" width="50" show-overflow-tooltip></el-table-column>
				<el-table-column type="index" align="cneter" width="50" show-overflow-tooltip></el-table-column>
				<el-table-column label="模板名称" prop="name" min-width="150" show-overflow-tooltip></el-table-column>
				<el-table-column label="模板类型" show-overflow-tooltip width="100">
					<template #default="scope">
						<span v-if="scope.row.type == 1" class="color-primary">站点模板</span>
						<span v-else-if="scope.row.type == 2" class="color-warning">栏目模板</span>
					</template>
				</el-table-column>
				<el-table-column label="平台类型" show-overflow-tooltip width="80">
					<template #default="scope">
						<span v-if="scope.row.catalog == 1" class="color-primary">桌面端</span>
						<span v-else-if="scope.row.catalog == 2" class="color-warning">移动端</span>
					</template>
				</el-table-column>
				<el-table-column label="状态" show-overflow-tooltip width="80">
					<template #default="scope">
						<span v-if="scope.row.status == '0'" class="color-primary">正常</span>
						<span v-else class="color-info">停用</span>
					</template>
				</el-table-column>
				<el-table-column label="备注" prop="remark" show-overflow-tooltip></el-table-column>
				<el-table-column label="创建者" prop="createBy" width="80" show-overflow-tooltip></el-table-column>
				<el-table-column label="更新时间" prop="createTime" width="180" show-overflow-tooltip>
					<template #default="scope">
						<span v-if="scope.row.updateTime == null">{{scope.row.createTime}}</span>
						<span v-else>{{scope.row.updateTime}}</span>
					</template>
				</el-table-column>
				<el-table-column label="操作" show-overflow-tooltip width="200"
					v-auths="['cms:template:edit', 'cms:template:remove', 'cms:template:released', 'cms:template:revoked']">
					<template #default="scope">
						<el-button v-auth="'cms:template:released'" type="primary" link @click="onReleasedTemplate(scope.row)">发布</el-button>
						<el-button v-if="scope.row.type == 1" v-auth="'cms:template:revoked'" type="primary" link @click="onRevokedTemplate(scope.row)">撤发</el-button>
						<el-button v-auth="'cms:template:edit'" type="primary" link @click="onOpenEditTemplate(scope.row)">修改</el-button>
						<el-button v-auth="'cms:template:remove'" type="primary" link @click="onTabelRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onPageSizeChange"
				@current-change="onPageCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30, 40, 50, 60]"
				v-model:current-page="search.pageNum"
				background
				v-model:page-size="search.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="tableData.total"
			>
			</el-pagination>
		</el-card>
		<AddTemplate ref="addTemplateRef" @submit="onTableSearch"/>
		<EditTemplate ref="editTemplateRef" @submit="onTableSearch"/>
	</div>
</template>

<script lang="ts">
import { ref, toRefs, reactive, onMounted, watch } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import AddTemplate from '/@/views/cms/channel/template/addTemplate.vue';
import EditTemplate from '/@/views/cms/channel/template/editTemplate.vue';
import { getTemplateList, delTemplate, releasedTemplate, revokedTemplate } from '/@/api/cms/channel/template/index';
export default {
	name: 'cms/template',
	components: { AddTemplate, EditTemplate },
	props: {
		// 绑定值
		model: {
			type: Object,
			default: () => {},
		},
	},
	setup(props) {
		const tableTemplateRef = ref();
		const addTemplateRef = ref();
		const editTemplateRef = ref();
		const state = reactive({
			search:{
				siteId: '',
				channelId: '',
				name: '',
				startTime: '',
				endTime: '',
				orderBy: 'createTime',
				isAsc: 'desc',
				pageNum: 1,
				pageSize: 10
			},
			tableData: {
				selections: [],//选中的行数据
				data: [],
				total: 0,
				loading: false
			}
		});

		const loadTableData = (params?: object) => {
			getTemplateList(Object.assign(state.search, params)).then((res) => {
				if(res.code == 0){
					state.tableData.data = res.data;
					state.tableData.total = res.total;
					tableTemplateRef.value.clearSelection();
				}
			});
		}

		//查询
		const onTableSearch = () => {
			loadTableData(state.search);
		}
		//重置
		const onResetSearch = () => {
			state.search.name = '';
			state.search.startTime = '';
			state.search.endTime = '';
			state.search.pageNum = 1;
			state.search.pageSize = 10;
			loadTableData(state.search);
		}
		// 监听表格选中行
		const onTableSelectionChange = (selections) =>{
			state.tableData.selections = selections;
		};
		// 每页显示数据大小改变
		const onPageSizeChange = (val: number) => {
			state.search.pageSize = val;
			loadTableData(state.search);
		};
		// 页码改变
		const onPageCurrentChange = (val: number) => {
			state.search.pageNum = val;
			loadTableData(state.search);
		};
		// 打开新增弹窗
		const onOpenAddTemplate = () => {
			addTemplateRef.value.openDialog(props.model);
		};
		// 打开编辑弹窗
		const onOpenEditTemplate = (row: object) => {
			row = row || state.tableData.selections[0];
			editTemplateRef.value.openDialog(row);
		};
		// 发布模板
		const onReleasedTemplate = (row: object) => {
			ElMessageBox.confirm('确定发布模板【' + row.name + '】吗？', '提示', {
				confirmButtonText: '发布',
				cancelButtonText: '取消',
				type: 'warning',
			}).then(() => {
				releasedTemplate({id: row.id}).then((res) => {
					if(res.code === 0){
						ElMessage.success('发布成功');
						loadTableData();
					}
				});
			}).catch(() => {});
		}
		const onRevokedTemplate = (row: object) => {
			ElMessageBox.confirm('确定撤销发布模板【' + row.name + '】吗？', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
			}).then(() => {
				revokedTemplate({id: row.id}).then((res) => {
					if(res.code === 0){
						ElMessage.success('撤销发布成功');
						loadTableData();
					}
				});
			}).catch(() => {});
		}
		// 删除选中行
		const onTabelRowsDel = () => {
			const ids = state.tableData.selections.map(item => item.id);
			if(ids && ids.length > 0){
				ElMessageBox.confirm('确定删除选中的模板吗？', '提示', {
					confirmButtonText: '删除',
					cancelButtonText: '取消',
					type: 'warning',
				}).then(() => {
					delTemplate(ids).then((res) => {
						if(res.code === 0){
							ElMessage.success('删除成功');
							loadTableData();
						}
					});
				}).catch(() => {});
			}
		};
		// 删除当前行
		const onTabelRowDel = (row: object) => {
			ElMessageBox.confirm('确定删除该条【' + row.name + '】模板吗？', '提示', {
				confirmButtonText: '删除',
				cancelButtonText: '取消',
				type: 'warning',
			}).then(() => {
				delTemplate([row.id]).then((res) => {
					if(res.code === 0){
						ElMessage.success('删除成功');
						loadTableData();
					}
				});
			}).catch(() => {});
		};
		// 页面加载时
		onMounted(() => {
			state.search.siteId = props.model.siteId;
			state.search.channelId = props.model.id;
			if(props.model.parentId == 0){
				state.search.channelId = '';
			}
			loadTableData();
		});
		
		//监听model数据
		watch(() => props.model.id, () => {
			state.search.siteId = props.model.siteId;
			state.search.channelId = props.model.id;
			if(props.model.parentId == 0){
				state.search.channelId = '';
			}
			loadTableData();
		}, {deep: true});
		
		return {
			tableTemplateRef,
			addTemplateRef,
			editTemplateRef,
			onTableSearch,
			onTableSelectionChange,
			onResetSearch,
			onPageSizeChange,
			onPageCurrentChange,
			onOpenAddTemplate,
			onOpenEditTemplate,
			onReleasedTemplate,
			onRevokedTemplate,
			onTabelRowsDel,
			onTabelRowDel,
			...toRefs(state),
		};
	},
};
</script>